<?php

define('IN_SEXMALL', true);

require(dirname(__FILE__) . '/includes/init.php');
$exc = new exchange(TABLEPRE.'goods_seckill', $db, 'act_id', 'act_name');

/*------------------------------------------------------ */
//-- 活动列表页
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'list')
{
	admin_priv('goods_seckill');

	$list = goods_seckill_list();

	$smarty->assign('seckill_list', 	$list['item']);
	$smarty->assign('filter',          	$list['filter']);
	$smarty->assign('record_count',    	$list['record_count']);
	$smarty->assign('page_count',      	$list['page_count']);

	/* 模板赋值 */
	$smarty->assign('full_page',   1);
	$smarty->assign('ur_here',     $_LANG['goods_seckill']);
	$smarty->assign('action_link', array('href' => 'goods_seckill.php?act=add', 'text' => '添加商品秒杀活动'));

	$sort_flag  = sort_flag($list['filter']);
	$smarty->assign($sort_flag['tag'], $sort_flag['img']);

	/* 显示商品列表页面 */
	assign_query_info();
	$smarty->display('goods_seckill_list.htm');
}

/*------------------------------------------------------ */
//-- 分页、排序、查询
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'query')
{
	$list = goods_seckill_list();

	$smarty->assign('seckill_list', 	$list['item']);
	$smarty->assign('filter',          	$list['filter']);
	$smarty->assign('record_count',    	$list['record_count']);
	$smarty->assign('page_count',      	$list['page_count']);

	$sort_flag  = sort_flag($list['filter']);
	$smarty->assign($sort_flag['tag'], $sort_flag['img']);

	make_json_result($smarty->fetch('goods_seckill_list.htm'), '',
	array('filter' => $list['filter'], 'page_count' => $list['page_count']));
}

/*------------------------------------------------------ */
//-- 删除
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove')
{
	admin_priv('goods_seckill');

	check_authz_json('goods_seckill');

	$act_id = intval($_GET['id']);
	$goods_seckill = goods_seckill_info($act_id);
	if (empty($goods_seckill))
	{
		make_json_error($_LANG['favourable_not_exist']);
	}
	$act_name = $goods_seckill['act_name'];
	$exc->drop($act_id);

	/* 记日志 */
	admin_log($act_name, 'remove', 'goods_seckill');

	/* 清除缓存 */
	clear_cache_files();

	$url = 'goods_seckill.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

	wap_header("Location: $url\n");
	exit;
}

/*------------------------------------------------------ */
//-- 批量操作
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'batch')
{
	/* 取得要操作的记录编号 */
	if (empty($_POST['checkboxes']))
	{
		sys_msg($_LANG['no_record_selected']);
	}
	else
	{
		/* 检查权限 */
		admin_priv('goods_seckill');

		$ids = $_POST['checkboxes'];

		if (isset($_POST['drop']))
		{
			/* 删除记录 */
			$sql = "DELETE FROM " . TABLEPRE.'goods_seckill' .
                    " WHERE act_id " . db_create_in($ids);
			$db->query($sql);

			/* 记日志 */
			admin_log('商品秒杀免邮活动', 'batch_remove', 'goods_seckill');

			/* 清除缓存 */
			clear_cache_files();

			$links[] = array('text' => $_LANG['back_favourable_list'], 'href' => 'goods_seckill.php?act=list&' . list_link_postfix());
			sys_msg('批量删除成功');
		}
	}
}

/*------------------------------------------------------ */
//-- 添加、编辑
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'add' || $_REQUEST['act'] == 'edit')
{
	/* 检查权限 */
	admin_priv('goods_seckill');

	/* 是否添加 */
	$is_add = $_REQUEST['act'] == 'add';
	$smarty->assign('form_action', $is_add ? 'insert' : 'update');

	/* 初始化、取得优惠活动信息 */
	if ($is_add)
	{
		$goods_seckill = array(
            'act_id'        => 0,
            'act_name'      => '',
        	'goods_id'		=> '',
        	'brands_id'		=> '',
            'start_time'    => date('Y-m-d', local_time() + 86400),
            'end_time'      => date('Y-m-d', local_time() + 4 * 86400),
        	'limit_num'		=> 0,
        	'is_open'		=> 1,
        	'is_free_postage'	=> 0,
		);
	}
	else
	{
		if (empty($_GET['act_id']))
		{
			sys_msg('invalid param');
		}
		$act_id = intval($_GET['act_id']);
		$goods_seckill = goods_seckill_info($act_id);
		if (empty($goods_seckill))
		{
			sys_msg($_LANG['favourable_not_exist']);
		}
	}
	$smarty->assign('goods_seckill', $goods_seckill);

	/* 赋值时间控件的语言 */
	$smarty->assign('cfg_lang', $_CFG['lang']);

	/* 显示模板 */
	if ($is_add)
	{
		$smarty->assign('ur_here', $_LANG['add_goods_seckill']);
	}
	else
	{
		$smarty->assign('ur_here', $_LANG['edit_goods_seckill']);
	}

	$href = 'goods_seckill.php?act=list';
	if (!$is_add)
	{
		$href .= '&' . list_link_postfix();
	}
	$smarty->assign('action_link', array('href' => $href, 'text' => '商品秒杀免邮活动列表'));
	assign_query_info();
	$smarty->display('goods_seckill_info.htm');
}

/*------------------------------------------------------ */
//-- 添加、编辑后提交
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'insert' || $_REQUEST['act'] == 'update')
{
	/* 检查权限 */
	admin_priv('goods_seckill');

	/* 是否添加 */
	$is_add = $_REQUEST['act'] == 'insert';

	/* 检查名称是否重复 */
	$act_name 		= sub_str($_POST['act_name'], 255, false);
	$goods_id		= empty($_POST['goods_id']) ? '' : trim($_POST['goods_id']);
	$brands_id		= empty($_POST['brands_id']) ? '' : trim($_POST['brands_id']);
	$limit_num		= empty($_POST['limit_num']) ? 0 : intval($_POST['limit_num']);
	$is_free_postage = empty($_POST['is_free_postage']) ? 0 : intval($_POST['is_free_postage']);
	$is_open		= empty($_POST['is_open']) ? 0 : intval($_POST['is_open']);

	if (!$exc->is_only('act_name', $act_name, intval($_POST['act_id'])))
	{
		sys_msg('活动名称已存在');
	}

	/* 提交值 */
	$goods_seckill = array(
        'act_id'        => intval($_POST['act_id']),
        'act_name'      => $act_name,
    	'goods_id'		=> $goods_id,
    	'brands_id'		=> $brands_id,
        'start_time'    => local_strtotime($_POST['start_time']),
        'end_time'      => local_strtotime($_POST['end_time']),
    	'limit_num'		=> $limit_num,
    	'is_free_postage'	=> $is_free_postage,
    	'is_open'		=> $is_open,
    	'add_time'		=> local_time(),
	);


	/* 保存数据 */
	if ($is_add)
	{
		$db->autoExecute(TABLEPRE.'goods_seckill', $goods_seckill, 'INSERT');
		$goods_seckill['act_id'] = $db->insert_id();
	}
	else
	{
		$db->autoExecute(TABLEPRE.'goods_seckill', $goods_seckill, 'UPDATE', "act_id = '".$goods_seckill['act_id']."'");
	}

	/* 记日志 */
	if ($is_add)
	{
		admin_log($goods_seckill['act_name'], 'add', 'goods_seckill');
	}
	else
	{
		admin_log($goods_seckill['act_name'], 'edit', 'goods_seckill');
	}

	/* 清除缓存 */
	clear_cache_files();

	/* 提示信息 */
	if ($is_add)
	{
		$links = array(
		array('href' => 'goods_seckill.php?act=add', 'text' => '继续添加商品秒杀免邮活动'),
		array('href' => 'goods_seckill.php?act=list', 'text' => '返回商品秒杀免邮活动列表')
		);
		sys_msg('添加成功', 0, $links);
	}
	else
	{
		$links = array(
		array('href' => 'goods_seckill.php?act=list&' . list_link_postfix(), 'text' => '返回商品秒杀免邮活动列表')
		);
		sys_msg('商品秒杀免邮活动编辑成功', 0, $links);
	}
}


/*
 * 取得优惠活动列表
 * @return   array
 */
function goods_seckill_list()
{
	$result = get_filter();
	if ($result === false)
	{
		/* 过滤条件 */
		$filter['keyword']    = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);
		if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
		{
			$filter['keyword'] = json_str_iconv($filter['keyword']);
		}
		$filter['is_open']    = empty($_REQUEST['is_open']) ? 0 : intval($_REQUEST['is_open']);
		$filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'act_id' : trim($_REQUEST['sort_by']);
		$filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

		$where = "";
		if (!empty($filter['keyword']))
		{
			$where .= " AND act_name LIKE '%" . mysql_like_quote($filter['keyword']) . "%'";
		}
		if ($filter['is_open'])
		{
			$now = local_time();
			$where .= " AND is_open = '".$filter['is_open']."'";
		}

		$sql = "SELECT COUNT(*) FROM " . TABLEPRE.'goods_seckill' .
                " WHERE 1 $where";
		$filter['record_count'] = $GLOBALS['db']->getOne($sql);

		/* 分页大小 */
		$filter = page_and_size($filter);

		/* 查询 */
		$sql = "SELECT act_id, act_name, goods_id, start_time, end_time," .
        		" limit_num, is_free_postage, is_open, add_time,brands_id".
                " FROM " . TABLEPRE.'goods_seckill' .
                " WHERE 1 $where ".
                " ORDER BY ".$filter['sort_by']." ". $filter['sort_order'].
                " LIMIT ". $filter['start'] .", ". $filter['page_size'];

		$filter['keyword'] = stripslashes($filter['keyword']);
		set_filter($filter, $sql);
	}
	else
	{
		$sql    = $result['sql'];
		$filter = $result['filter'];
	}
	$res = $GLOBALS['db']->query($sql);

	$list = array();
	while ($row = $GLOBALS['db']->fetchRow($res))
	{
		$row['start_time']  = local_date('Y-m-d H:i', $row['start_time']);
		$row['end_time']    = local_date('Y-m-d H:i', $row['end_time']);

		$list[] = $row;
	}

	return array('item' => $list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

/**
 * 取得优惠活动信息
 * @param   int     $act_id     活动id
 * @return  array
 */
function goods_seckill_info($act_id)
{
	$sql = "SELECT act_id, act_name, goods_id,start_time, end_time," .
    		" limit_num, is_free_postage, is_open, add_time,brands_id" .
    		" FROM " . TABLEPRE.'goods_seckill' .
            " WHERE act_id = '$act_id'";
	$row = $GLOBALS['db']->getRow($sql);
	if (!empty($row))
	{
		$row['start_time'] = local_date('Y-m-d H:i:s', $row['start_time']);
		$row['end_time'] = local_date('Y-m-d H:i:s', $row['end_time']);
	}

	return $row;
}

?>